#include "Sprite.h"
#include "GL/glfw.h"

bool SpriteResource::Load( const std::string & fn ){
	GLFWimage data;
	if (!glfwReadImage( fn.c_str(), &data , 0)) {
		mHandle = 0;
		return false;
	}
	
	//Data is in data variable.
	//Load the texture with opengl.
	glGenTextures(1, &mHandle);

	glBindTexture(GL_TEXTURE_2D, mHandle);

	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

	printf("%d %d %d\n", data.Width, data.Height, data.BytesPerPixel);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data.Width, data.Height, 0, data.Format, GL_UNSIGNED_BYTE, data.Data);

	glfwFreeImage(&data);

	return true;
}